package com.etc.drug.service.impl;

import cn.hutool.json.JSONUtil;
import com.etc.common.mapper.LogMapper;
import com.etc.common.mapper.OutpatientServiceMapper;
import com.etc.common.utils.Result;
import com.etc.common.utils.UUIDUtil;
import com.etc.drug.entity.Doctor;
import com.etc.drug.entity.OutBound;
import com.etc.drug.entity.Prescription;
import com.etc.drug.mapper.DrugMapper;
import com.etc.drug.mapper.PrescriptionMapper;
import com.etc.drug.service.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.etc.common.utils.RedisConstants.CACHE_DRUGNUM;
import static com.etc.common.utils.RedisConstants.CACHE_PRESCRIPTION;


@Service
public class PrescriptionServiceImpl implements PrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Resource
    private OutpatientServiceMapper opServiceMapper;
    @Resource
    private DrugMapper drugMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private LogMapper logMapper;

    /**
     * 根据门诊Id查询处方
     */
    @Override
    public List<Prescription> queryByOsId(String osId) {
        List<Prescription> list = prescriptionMapper.queryByOsId(osId);
        return list;
    }

    @Override
    @Transactional
    public Result insert(String patientId,String osId,String prescriptionUsage,String auditorId, List<OutBound> outBoundList, String operatorId) {
        //设置uuid
        String prescriptionId= UUIDUtil.getUUID();
        //如果前端没有传门诊Id进来，则根据病人Id获取最近一次的门诊编号
        if (osId == null) {
            //根据病人Id获取最近一次的门诊编号
            osId=opServiceMapper.selectOsId(patientId);
        }
        for (OutBound outBound : outBoundList) {
            outBound.setPrescriptionId(prescriptionId);
            //新增出库记录
            drugMapper.outbound(UUIDUtil.getUUID(), outBound.getPrescriptionId(), outBound.getDrugId(),
                    outBound.getDrugValidity(), outBound.getOutBoundNum());
        }
        //删除redis中的缓存,药品库存缓存
        stringRedisTemplate.delete(CACHE_DRUGNUM);
        //删除redis，抓药人的redis缓存
        stringRedisTemplate.delete(CACHE_PRESCRIPTION + ":auditor:" + auditorId);
        //记录日志
        logMapper.insert(UUIDUtil.getUUID(), operatorId, "新增处方" + patientId);
        //设置处方状态为默认通过,病人未开药
        return Result.ok(prescriptionMapper.insert(prescriptionId,osId,prescriptionUsage,auditorId,1,0));
    }

    @Override
    public List<Prescription> queryByDocId(Integer pageNum, Integer pageSize, String docId) {
        Boolean isExist = stringRedisTemplate.hasKey(CACHE_PRESCRIPTION + ":auditor:" + docId);
        if (!isExist) {
            //无值，查询数据库
            List<Prescription> list = prescriptionMapper.queryByDocId(docId);
            for (Prescription prescription : list) {
                //转化成json
                String jsonStr = JSONUtil.toJsonStr(prescription);
                //写入redis
                stringRedisTemplate.opsForList().leftPush(CACHE_PRESCRIPTION + ":auditor:" + docId, jsonStr);
            }
        }
        //获取redis中的处方列表
        List<String> json = stringRedisTemplate.opsForList().range(CACHE_PRESCRIPTION + ":auditor:" + docId,
                pageSize * (pageNum - 1), pageSize * (pageNum - 1) + pageSize - 1);
        List<Prescription> list = JSONUtil.toList(json.toString(), Prescription.class);
        return list;
    }

    @Override
    public Result update(Prescription prescription, String operatorId) {
        //删除redis，抓药人的redis缓存
        stringRedisTemplate.delete(CACHE_PRESCRIPTION + ":auditor:" + prescription.getAuditorId());
        //记录日志
        logMapper.insert(UUIDUtil.getUUID(), operatorId, "修改了处方：" + prescription.getPrescriptionId());
        return Result.ok(prescriptionMapper.update(prescription));
    }

    @Override
    public Result breakDrug(String prescriptionId, String operatorId) {
        //删除redis，抓药人的redis缓存
        stringRedisTemplate.delete(CACHE_PRESCRIPTION + ":auditor:" + prescriptionId);
        //记录日志
        logMapper.insert(UUIDUtil.getUUID(), operatorId, "驳回了ID为：" + prescriptionId + "的处方单");
        return Result.ok(prescriptionMapper.updateStatus(prescriptionId,0,0));
    }

    @Override
    public List<Doctor> queryDrugDoc() {
        return prescriptionMapper.queryDrugDoc();
    }

}
